package com.voxel.sense.infomsg.module.user.regist;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.example.com.common.app.BViewActivity;
import com.example.com.common.app.BaseContract;
import com.example.com.common.utils.SnackbarUtils;
import com.example.com.common.utils.ToastUtil;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.RegionBean;
import com.voxel.sense.infomsg.view.titlebar.BaseTitleBar;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author ChenYiYao
 * @date 2018/5/14
 * des : 注册View层
 */
public class RegisterActivity extends BViewActivity<RegisterContract.Presenter> implements RegisterContract.View {
    @BindView(R.id.titlebar)
    BaseTitleBar titlebar;
    @BindView(R.id.btn_region)
    Button btn_region;
    @BindView(R.id.tv_region_tip)
    TextView tv_region_tip;
    @BindView(R.id.et_account)
    EditText et_account;
    @BindView(R.id.tv_account_tip)
    TextView tv_account_tip;
    @BindView(R.id.et_psw)
    EditText et_psw;
    @BindView(R.id.et_psw_again)
    EditText et_psw_again;
    @BindView(R.id.tv_psw_tip)
    TextView tv_psw_tip;
    @BindView(R.id.et_idcar)
    EditText et_idcar;
    @BindView(R.id.et_real_name)
    EditText et_real_name;
    @BindView(R.id.et_phone)
    EditText et_phone;
    @BindView(R.id.tv_phone_tip)
    TextView tv_phone_tip;
    @BindView(R.id.et_email)
    EditText et_email;

    private String mRegionCode;

    public static void show(Context context) {
        context.startActivity(new Intent(context, RegisterActivity.class));
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_regist;
    }

    @Override
    protected RegisterContract.Presenter initPresenter() {
        return new RegisterPresenter(this);
    }

    @Override
    protected void initData() {
        super.initData();
        titlebar.setTitleText("注册");
    }

    @OnClick({R.id.btn_region, R.id.btn_submit})
    void clickEvent(View v) {
        if (v.getId() == R.id.btn_region) {
            //不需要选择省市区
//            mPresenter.loadProvinceData();
        } else if (v.getId() == R.id.btn_submit) {
            mPresenter.doRegister(et_account.getText().toString().trim(), et_psw.getText().toString().trim(), et_psw_again.getText().toString(),
                    mRegionCode, et_idcar.getText().toString(), et_real_name.getText().toString(), et_phone.getText().toString(),
                    et_email.getText().toString());
        }
    }

    @Override
    public void onShowRegionTip(String desc) {

    }

    @Override
    public void onShowAccountTip(String desc) {

    }

    @Override
    public void onShowPswTip(String desc) {

    }

    @Override
    public void onShowPhoneTip(String desc) {

    }

    @Override
    public void showSnackBarWarning(String str) {
        super.showSnackBarWarning(str);
        SnackbarUtils.with(titlebar).setMessage(str).showWarning();
    }

    @Override
    public void onShowRegionPickerView(List province, List city, List district) {
        OptionsPickerView pvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                RegionBean bean = (RegionBean) province.get(options1);
                String province = bean.getName();
                String city = "";
                String district = "";
                if (bean.getChild() != null && bean.getChild().size() - 1 >= options2) {
                    RegionBean.ChildBeanX childBeanX = bean.getChild().get(options2);
                    List<RegionBean.ChildBeanX.ChildBean> child = childBeanX.getChild();
                    city = childBeanX.getName();
                    if (child != null && child.size() - 1 >= options3) {
                        mRegionCode = child.get(options3).getId();
                        district = child.get(options3).getName();
                    } else {
                        mRegionCode = childBeanX.getId();
                    }
                } else {
                    mRegionCode = bean.getId();
                }
                btn_region.setText(province + " " + city + " " + district);
            }
        })

                .setTitleText("城市选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .build();
        pvOptions.setPicker(province, city, district);//三级选择器
        pvOptions.show();
    }

    @Override
    public void onRegistSuccess() {
        ToastUtil.showToastLong(this, "注册成功，等待审核！");
        finish();
    }

}
